/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.sorbonne.inf5.yams.vue;

import fr.sorbonne.inf5.yams.controleur.ControleurJeu;
import java.awt.Color;
import java.awt.Component;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

/**
 * Classe qui se charge de l'affichage
 * des dès
 * @author Dubos Romain
 * @author Oukherfellah Riad
 */
public class VueDes {

    //Objets
    private JPanel panel;
    private JButton de1, de2, de3, de4, de5;
    //IMAGES
    private static String DE1 = "images/DE1.jpg";
    private static String DE1V = "images/DE1_V.jpg";
    private static String DE2 = "images/DE2.jpg";
    private static String DE2V = "images/DE2_V.jpg";
    private static String DE3 = "images/DE3.jpg";
    private static String DE3V = "images/DE3_V.jpg";
    private static String DE4 = "images/DE4.jpg";
    private static String DE4V = "images/DE4_V.jpg";
    private static String DE5 = "images/DE5.jpg";
    private static String DE5V = "images/DE5_V.jpg";
    private static String DE6 = "images/DE6.jpg";
    private static String DE6V = "images/DE6_V.jpg";
    public static final String D1 = "de1";
    public static final String D2 = "de2";
    public static final String D3 = "de3";
    public static final String D4 = "de4";
    public static final String D5 = "de5";
    //TABlEAUX DE CORRESPONDANCE CHIFFRE/IMAGE
    private ImageIcon[] imagesDes;
    private ImageIcon[] imagesDesValide;
    private JButton[] des;

    /**
     * Constructeur
     */
    public VueDes() {
        this.panel = new JPanel();

        this.de1 = new JButton(new ImageIcon(DE1));
        this.de1.setActionCommand(D1);
        this.de1.setBorder(null);
        this.de2 = new JButton(new ImageIcon(DE1));
        this.de2.setActionCommand(D2);
        this.de2.setBorder(null);
        this.de3 = new JButton(new ImageIcon(DE1));
        this.de3.setActionCommand(D3);
        this.de3.setBorder(null);
        this.de4 = new JButton(new ImageIcon(DE1));
        this.de4.setActionCommand(D4);
        this.de4.setBorder(null);
        this.de5 = new JButton(new ImageIcon(DE1));
        this.de5.setActionCommand(D5);
        this.de5.setBorder(null);

        this.des = new JButton[6];
        this.des[1] = this.de1;
        this.des[2] = this.de2;
        this.des[3] = this.de3;
        this.des[4] = this.de4;
        this.des[5] = this.de5;


        this.panel.add(de1);
        this.panel.add(de2);
        this.panel.add(de3);
        this.panel.add(de4);
        this.panel.add(de5);
        panel.setBackground(Color.WHITE);

        this.initTab();
    }

    /**
     * Initilise les tableaux de correspondance
     * entre numéro et image
     */
    private void initTab() {
        //On ne remplit pas la case 0 pour faciliter la correspondance (1 à 6)
        this.imagesDes = new ImageIcon[7]; //dés normaux
        this.imagesDes[1] = new ImageIcon(DE1);
        this.imagesDes[2] = new ImageIcon(DE2);
        this.imagesDes[3] = new ImageIcon(DE3);
        this.imagesDes[4] = new ImageIcon(DE4);
        this.imagesDes[5] = new ImageIcon(DE5);
        this.imagesDes[6] = new ImageIcon(DE6);

        this.imagesDesValide = new ImageIcon[7]; //dès entourés (validés)
        this.imagesDesValide[1] = new ImageIcon(DE1V);
        this.imagesDesValide[2] = new ImageIcon(DE2V);
        this.imagesDesValide[3] = new ImageIcon(DE3V);
        this.imagesDesValide[4] = new ImageIcon(DE4V);
        this.imagesDesValide[5] = new ImageIcon(DE5V);
        this.imagesDesValide[6] = new ImageIcon(DE6V);
    }

    /**
     * Abonne les dés au controleur
     * @param ctrl controleur du jeu
     */
    public void abonnement(ControleurJeu ctrl) {
        this.de1.addActionListener(ctrl);
        this.de2.addActionListener(ctrl);
        this.de3.addActionListener(ctrl);
        this.de4.addActionListener(ctrl);
        this.de5.addActionListener(ctrl);
    }

    /**
     * Renvoie le panel contenant les dès
     * @return JPanel
     */
    public JPanel getPanel() {
        return this.panel;
    }

    /**
     * Re-initialise l'affichage des dés à 1
     */
    public void initialiseDesA1() {
        this.de1.setIcon(new ImageIcon(DE1));
        this.de1.setBorder(null);
        this.de2.setIcon(new ImageIcon(DE1));
        this.de2.setBorder(null);
        this.de3.setIcon(new ImageIcon(DE1));
        this.de3.setBorder(null);
        this.de4.setIcon(new ImageIcon(DE1));
        this.de4.setBorder(null);
        this.de5.setIcon(new ImageIcon(DE1));
        this.de5.setBorder(null);
    }

    /**
     * Permet de rendre inutilisable les boutons
     * des dés
     */
    public void griserVue() {
        Component[] composants = this.panel.getComponents();
        for (int i = 0; i < composants.length; i++) {
            if (composants[i] instanceof JButton) {
                composants[i].setEnabled(false);
            }
        }
    }

    /**
     * Permet de rendre utilisable les boutons
     * des dés
     */
    public void deGriserVue() {
        Component[] composants = this.panel.getComponents();
        for (int i = 0; i < composants.length; i++) {
            if (composants[i] instanceof JButton) {
                composants[i].setEnabled(true);
            }
        }
    }

    /**
     * Fait la correspondance entre une face d'un dé et son image
     * @param numFace numéro de la face
     * @param valide dés bloqué ou non
     * @return String du lien vers l'image adéquate
     */
    public ImageIcon renvoyerImageCorrespondante(int numFace, boolean valide) {

        if (!valide) {
            return this.imagesDes[numFace];
        }

        return this.imagesDesValide[numFace];

    }

    /**
     * Charge une nouvelle image dans le bouton
     * @param image Image a affiché
     * @param numDe numero du dé à changer
     */
    public void changerDe(ImageIcon image, int numDe) {
        des[numDe].setIcon(image);
        des[numDe].repaint();
        des[numDe].setBorder(null);
    }
}
